import "./miniui.css";
import { mini } from "./miniui";
import "./report";

declare global {
    interface Window {
        mini_debugger: boolean;
        jQuery: JQueryStatic;
        $: JQueryStatic;
    }
    interface Array<T> {
        add(item: T): this;
        enqueue(item: T): void;
        getRange(start: number, end: number): Array<T>;
        addRange(array: Array<T>): this;
        clear(): this;
        clone(): Array<T>;
        contains(item: T): boolean;
        indexOf(item: T, from: number): number;
        dequeue(): T;
        insert(index: number, item: T): this;
        insertRange(index: number, items: T): this;
        remove(item: T): boolean;
        removeAt(index: number): T;
        removeRange(items: Array<T>): void;
    }
    interface StringConstructor {
        format(format: string, ...args: any): string;
    }
    interface String {
        trim(): string;
    }
    interface Date {
        getHalfYear?: () => number;
        getQuarter?: () => number;
    }
}

if (typeof window.mini_debugger == 'undefined') {
    window.mini_debugger == false;
}


type ThemeSkins = 'blue' | 'blue2003' | 'blue2010' | 'bootstrap' | 'cupertino' | 'default' | 'gray' | 'jqueryui-cupertino' | 'jqueryui-smoothness' | 'metro-blue' | 'metro-gray' | 'metro-green' | 'metro-orange' | 'metro-white' | 'olive2003' | 'pure';
type ThemeSize = 'large' | 'medium' | 'default'
() => {

}
let bootPATH = __CreateJSPath("miniui.js");
let themeSkin = mini.Cookie.get("miniuiSkin") || 'gray';             //skin cookie   cupertino
let themeSize: ThemeSize = (mini.Cookie.get("miniuiMode") || 'medium') as ThemeSize;           //size cookie     medium   
appendLink("mini_font", bootPATH + "miniui/res/fonts/font-awesome/css/font-awesome.min.css");
appendLink("mini_css", bootPATH + 'miniui/themes/default/miniui.css');

//skin
let mini_theme_skin: HTMLLinkElement | null = null;
if (themeSkin != 'default') {
    mini_theme_skin = appendLink("mini_theme_skin", bootPATH + 'miniui/themes/' + themeSkin + '/skin.css');
}
//size
let mini_theme_size: HTMLLinkElement | null = null;
if (themeSize != 'default') {
    mini_theme_size = appendLink('mini_theme_size', bootPATH + 'miniui/themes/default/' + themeSize + '-mode.css');
}
//icon
appendLink('mini_icon', bootPATH + 'miniui/themes/icons.css');

function appendLink(id: string, href: string, before?: HTMLElement): HTMLLinkElement {
    let lnk = document.createElement("link") as HTMLLinkElement;
    lnk.id = id;
    lnk.href = href;
    lnk.type = "text/css"
    lnk.rel = "stylesheet"
    if (before) {
        before.before(lnk);
    } else {
        let links: NodeListOf<HTMLElement> = document.getElementsByTagName("head")[0].querySelectorAll("link");
        links = links.length == 0 ? document.getElementsByTagName("head")[0].querySelectorAll("meta") : links;
        links[links.length - 1].after(lnk);
    }
    return lnk
}
function __CreateJSPath(js: string) {
    let scripts = document.getElementsByTagName("script");
    let path = "";
    for (let i = 0, l = scripts.length; i < l; i++) {
        let src = scripts[i].src;
        if (src.indexOf(js) != -1) {
            let ss = src.split(js);
            path = ss[0];
            break;
        }
    }
    let href = location.href;
    href = href.split("#")[0];
    href = href.split("?")[0];
    let ss = href.split("/");
    ss.length = ss.length - 1;
    href = ss.join("/");
    if (path.indexOf("https:") == -1 && path.indexOf("http:") == -1 && path.indexOf("file:") == -1 && path.indexOf("\/") != 0) {
        path = href + "/" + path;
    }
    return path;
}

function setThemeSkin(skin: ThemeSkins) {
    if (skin != 'default') {
        if (!mini_theme_skin) {
            let before = mini_theme_size ? mini_theme_size : document.getElementById('mini_icon')!;
            mini_theme_skin = appendLink("mini_theme_skin", bootPATH + "miniui/themes/" + skin + "/skin.css", before);
        } else {
            mini_theme_skin.href = bootPATH + 'miniui/themes/' + skin + '/skin.css';
        }
    } else {
        document.head.removeChild(mini_theme_skin!)
        mini_theme_skin = null
    }
}
function setThemeSize(size: ThemeSize) {
    if (size !== 'default') {
        if (!mini_theme_size) {
            mini_theme_size = appendLink("mini_theme_size", bootPATH + 'miniui/themes/default/' + size + '-mode.css', document.getElementById("mini_icon")!)
            return;
        } else {
            mini_theme_size.href = bootPATH + 'miniui/themes/default/' + size + '-mode.css';
        }
    } else {
        document.head.removeChild(mini_theme_size!);
        mini_theme_size = null;
    }
}
export {
    mini,
    setThemeSize,
    setThemeSkin,
    themeSkin,
    themeSize,
    ThemeSkins,
    ThemeSize
}
